<template>
	<view class="container">
		<form>
			<view class="input-row" v-if="!project">
				<view style="width: 200rpx;">所属工地：</view>
				<picker disabled="true" style="width: 450rpx;text-align: center;" @change="bindProtectChanges" range-key="cn"
					:data-index="22" :data-id="cprojects[cprojectIndex].id" :value="cprojectIndex" :range="cprojects">
					<view class="uni-input">{{cprojects[cprojectIndex].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="cprojects[presultindex].id" hidden />
				</picker>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">整改类型：</view>
				<picker disabled="true" style="width: 500rpx;text-align: center;" @change="bindPickerChanges" range-key="cn"
					:data-index="22" :data-id="rftypes[rftypeIndex].id" :value="rftypeIndex" :range="rftypes">
					<view class="uni-input">{{rftypes[rftypeIndex].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="rftypes[rftypeIndex].id" hidden />
				</picker>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">整改回复一：</view>
				<textarea style="width: 500rpx;" auto-height="true" type="text" v-model="rfreply1" />
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">整改照片一：</view>
				<image :src="photo1" v-if="photo1!='null'&&photo1!=null" @click="imgPreview(photo1)" style="width: 200rpx;height: 200rpx;"></image>
				<view @click="chooseImg1" v-if="photo1=='null' ||photo1==null" style="height: 200rpx;width: 200rpx;">
					<image src="/static/images/add1.png" style="width: 200rpx;height: 200rpx;"></image>
				</view>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">整改回复二：</view>
				<textarea style="width: 500rpx;" type="text" v-model="rfreply2" />
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">整改照片二：</view>
				<image :src="photo2" v-if="photo2!='null'&&photo2!=null" @click="imgPreview(photo2)" style="width: 200rpx;height: 200rpx;"></image>
				
				<view @click="chooseImg2" v-if="photo2=='null'||photo2==null" style="height: 200rpx;width: 500rpx;">
					<image src="/static/images/add1.png" style="width: 200rpx;height: 200rpx;"></image>
				</view>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">整改回复三：</view>
				<textarea style="width: 500rpx;" type="text" v-model="rfreply3" />
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">整改照片三：</view>
				<image :src="photo3" v-if="photo3!='null'&&photo3!=null" @click="imgPreview(photo3)" style="width: 200rpx;height: 200rpx;"></image>
				
				<view @click="chooseImg3" v-if="photo3=='null' ||photo3==null" style="height: 200rpx;width: 500rpx;">
					<image src="/static/images/add1.png" style="width: 200rpx;height: 200rpx;"></image>
				</view>
			</view>
			<view style="display: flex;align-items: center;text-align: center;">
				<button @click="submitForm"
					style="width: 500rpx;height: 80rpx;background-color: #00aaff;color: #fff;">提交</button>
			</view>
		</form>
	</view>
</template>

<script>
	import {
		request
	} from '@/request/index.js';
	export default {
		data() {
			return {
				pphoto1: '',
				pphoto2: '',
				pmode: '',
				pModes: [],
				index: 0,
				presult: '',
				presults: [],
				presultindex: 0,
				cproject: '',
				cprojects: [],
				cprojectIndex: 0,
				project: '',
				userInfo: {},
				detailList: [],
				
				photo1:'',
				photo2:'',
				photo3:'',
				irid:'',
				rfphoto1:'',
				rfphoto2:'',
				rfphoto3:'',
				rfreply1:'',
				rfreply2:'',
				rfreply3:'',
				rftypes:[],
				rftype:'',
				rftypeIndex:0,
				pid:'',
				ptype:''
			};
		},
		onLoad(option) {
			var userInfo = uni.getStorageSync('user');
			// if (userInfo == null || userInfo == 'null' || userInfo == '') {
			// 	uni.showToast({
			// 		title: "请先登录",
			// 		success() {
			// 			uni.reLaunch({
			// 				url: "/pages/login/login"
			// 			})
			// 		}
			// 	})

			// }
			var _self = this;
			_self.userInfo = userInfo
	
			this.irid = option.id
			this.pid = option.pid
			this.ptype = option.ptype
			this.getReformInfo(option.id)
			
			this.getRfTypes()
			this.getPresults()
			this.getProjects()



		},
		onPullDownRefresh(){
			this.getReformInfo(this.irid)
		},
		methods: {
			imgPreview(url) {
				uni.previewImage({
					urls: [url]
				})
			},
			getReformInfo(id){
				request({
					url: "/thirdGrid/query/c_sirectify-m_sirectify?query_irid="+id,
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
			
					if (res.data.success) {
						var item={}
						if(res.data.rows.length>0){
							var item = res.data.rows[0]
							// if(item.rfphoto1=='null'){
							// 	item.rfphoto1 = '/static/images/no.png'
							// }
							// if(item.rfphoto2=='null'){
							// 	item.rfphoto2 = '/static/images/no.png'
							// }
							// if(item.rfphoto3=='null'){
							// 	item.rfphoto3 = '/static/images/no.png'
							// }
							if(item.rfreply1=='null'){
								item.rfreply1 = ''
							}
							if(item.rfreply2=='null'){
								item.rfreply2 = ''
							}
							if(item.rfreply3=='null'){
								item.rfreply3 = ''
							}
							this.rfphoto1 = item.rfphoto1
							this.photo1 = item.rfphoto1
							this.rfphoto2 = item.rfphoto2
							this.photo2 = item.rfphoto2
							this.rfphoto3 = item.rfphoto3
							this.photo3 = item.rfphoto3
							this.rfreply1 = item.rfreply1
							this.rfreply2 = item.rfreply2
							this.rfreply3 = item.rfreply3
							this.cproject = item.cproject_val
							for(var i = 0;i<this.cprojects.length;i++){
								if(item.cproject_val == this.cprojects[i].id){
									this.cprojectIndex = i
									// this.cproject = this.cprojects[i].id
								}
							}
							this.rftype = item.rftype_val
							for(var j = 0;j<this.rftypes.length;j++){
								if(item.rftype_val == this.rftypes[j].id){
									this.rftypeIndex = j
									// this.rftype = this.rftypes[j].id
								}
							}
						}
						
						
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			
			
			bindPickerChanges(e) {
			
				this.rftypeIndex = e.target.value
				this.rftype = this.rftypes[this.rftypeIndex].id
			},
			bindPresultChanges(e) {
	
				this.presultindex = e.target.value
				this.presult = this.presults[this.presultindex].id
			},
			bindProtectChanges(e) {
				this.cprojectIndex = e.target.value
				this.cproject = this.cprojects[this.cprojectIndex].id
			},
			removeDetail(index) {
			

				this.detailList.splice(index, 1);
			},
			updateReform() {
				var data = {}
				var that = this
				var location = uni.getStorageSync("location")
				
				// 使用工具函数验证并修正坐标值
				const { validateCoordinates } = require('@/utils/index.js');
				const coords = validateCoordinates(location?.latitude, location?.longitude);
				
				data.longitude = coords.longitude;
				data.latitude = coords.latitude;
				
				data.irid = this.irid;
				data.rftype = this.rftype;
				data.rfphoto1 = this.rfphoto1;
				data.rfphoto2 = this.rfphoto2;
				data.rfphoto3 = this.rfphoto3;
				data.rfreply1 = this.rfreply1;
				data.rfreply2 = this.rfreply2;
				data.rfreply3 = this.rfreply3;
		
				
				request({
					url: "/thirdForm/doUpdate/c_sirectify",
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
				
					if (res.data.code == '200') {
						// uni.showToast({
						// 	title: res.data.msg,
						// })
						uni.navigateTo({
							url:"/pages/updateCpatrol/updateCpatrol?pid="+this.pid+"&ptype="+this.ptype
						})
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},

			getRfTypes() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_cpatrol-rftype",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					
					if (res.data.code == '200') {
						that.rftypes = []
						for (var i = 0; i < res.data.data.length; i++) {
							var temp = {}
							temp.id = res.data.data[i].id
							temp.cn = res.data.data[i].cn
							if (temp.id != '') {
								that.rftypes.push(temp)
							}
						}
						that.rftype = that.rftypes[that.rftypeIndex].id
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			getPresults() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_cpatrol-presult",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
				
					if (res.data.code == '200') {
						that.presults = []
						for (var i = 0; i < res.data.data.length; i++) {

							var result = {}
							result.id = res.data.data[i].id
							result.cn = res.data.data[i].cn
							if (result.id != '') {
								that.presults.push(result)
							}

						}
						that.presult = that.presults[that.presultindex].id

					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			getProjects() {
				var that = this
				request({
					url: "/thirdWidget/findJson?code=c_cpatrol&field=cproject",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
				
					if (res.data.success == true) {
						that.cprojects = []
						for (var i = 0; i < res.data.rows.length; i++) {

							var project = {}
							project.id = res.data.rows[i].cpid
							project.cn = res.data.rows[i].csname

							that.cprojects.push(project)
						}
						that.cproject = that.cprojects[that.cprojectIndex].id

					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			submitForm() {
				// 这里可以添加提交表单的逻辑
				this.updateReform()
			},
			godetail() {
				uni.redirectTo({
					url: "/pages/sipdetail/sipdetail"
				})
			},
			chooseImg1() {
				var that = this
				uni.chooseImage({
					count: 1, // 默认9，设置图片的数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						(tempFilePaths)
						that.photo1 = tempFilePaths[0]
						const uploadTask = uni.uploadFile({
							url: 'https://qscs.qtech2000.cn:8443/thirdUpload/img?name=pphoto1_file', // 开发者服务器的URL
							filePath: tempFilePaths[0],
							name: 'pphoto1_file', // 必填，文件对应的 key
							header: {
								'cookie': uni.getStorageSync('cookie')
							},
							success: (uploadFileRes) => {
								('upload success:', uploadFileRes);
								var data = JSON.parse(uploadFileRes.data)
								that.rfphoto1 = data.data.fileName
							},
							fail: (uploadFileErr) => {
								console.error('upload fail:', uploadFileErr);
							}
						});
						// 监听上传进度变化
						uploadTask.onProgressUpdate((res) => {
						
						});
					},
					fail: (chooseImageErr) => {
						console.error('choose image fail:', chooseImageErr);
					}
				});
			},
			chooseImg2() {
				var that = this
				uni.chooseImage({
					count: 1, // 默认9，设置图片的数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						(tempFilePaths)
						that.photo2 = tempFilePaths[0]
						const uploadTask = uni.uploadFile({
							url: 'https://qscs.qtech2000.cn:8443/thirdUpload/img?name=pphoto2_file', // 开发者服务器的URL
							filePath: tempFilePaths[0],
							header: {
								'cookie': uni.getStorageSync('cookie')
							},
							name: 'pphoto2_file', // 必填，文件对应的 key
							success: (uploadFileRes) => {
								('upload success:', uploadFileRes);
								var data = JSON.parse(uploadFileRes.data)
								that.rfphoto2 = data.data.fileName
							},
							fail: (uploadFileErr) => {
								console.error('upload fail:', uploadFileErr);
							}
						});
			
						uploadTask.onProgressUpdate((res) => {
					
						});
					},
					fail: (chooseImageErr) => {
						console.error('choose image fail:', chooseImageErr);
					}
				});
			},
			chooseImg3() {
					var that = this
					uni.chooseImage({
						count: 1, // 默认9，设置图片的数量
						sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
						success: (chooseImageRes) => {
							const tempFilePaths = chooseImageRes.tempFilePaths;
							(tempFilePaths)
							that.photo3 = tempFilePaths[0]
							const uploadTask = uni.uploadFile({
								url: 'https://qscs.qtech2000.cn:8443/thirdUpload/img?name=pphoto3_file', // 开发者服务器的URL
								filePath: tempFilePaths[0],
								header: {
									'cookie': uni.getStorageSync('cookie')
								},
								name: 'pphoto3_file', // 必填，文件对应的 key
								success: (uploadFileRes) => {
									('upload success:', uploadFileRes);
									var data = JSON.parse(uploadFileRes.data)
									that.rfphoto3 = data.data.fileName
								},
								fail: (uploadFileErr) => {
									console.error('upload fail:', uploadFileErr);
								}
							});
				
							uploadTask.onProgressUpdate((res) => {
								
							});
						},
						fail: (chooseImageErr) => {
							console.error('choose image fail:', chooseImageErr);
						}
					});
				}
		},

	};
</script>

<style>
	.container {
		padding: 20px;
	}

	.input-row {
		margin-bottom: 20px;
		display: flex;

	}

	.input-row text {
		flex-grow: 1;
		margin-right: 10px;
	}
</style>